﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[AddComponentMenu("Behavior/Sine")]
public class BhvSine : SineBase
{
    public enum Movement
    {
        horizontal,
        vertical,
        depth,
        scale,
        scaleX,
        scaleY,
        scaleZ,
        eulerX,
        eulerY,
        eulerZ,
        value,
    }


    public Movement movement = Movement.horizontal;


    protected override void AfterInit()
    {

        switch (this.movement)
        {
            case Movement.horizontal:
                this.initialValue = transform.localPosition.x;
                break;
            case Movement.vertical:
                this.initialValue = transform.localPosition.y;
                break;
            case Movement.depth:
                this.initialValue = transform.localPosition.z;
                break;
            case Movement.scale:
                this.initialValue = transform.localScale.x;
                break;
            case Movement.scaleX:
                this.initialValue = transform.localScale.x;
                break;
            case Movement.scaleY:
                this.initialValue = transform.localScale.y;
                break;
            case Movement.scaleZ:
                this.initialValue = transform.localScale.z;
                break;
            case Movement.eulerX:
                this.initialValue = transform.localRotation.eulerAngles.x;
                break;
            case Movement.eulerY:
                this.initialValue = transform.localRotation.eulerAngles.y;
                break;
            case Movement.eulerZ:
                this.initialValue = transform.localRotation.eulerAngles.z;
                break;
            case Movement.value:
                //value only, leave at 0
                this.initialValue = 0;
                break;
            default:
                print("Invalid sin movement type");
                break;
        }

        this.lastKnownValue = this.initialValue;
        this.lastKnownValue2 = this.initialValue2;
    }

    public void SetMovement(Movement m)
    {
        movement = m;
        AfterInit();
    }


    protected override void UpdateFromPhase(float value)
    {
        var waveRes = value;
        switch (this.movement)
        {
            case Movement.horizontal:
                if (transform.localPosition.x != this.lastKnownValue)
                {
                    this.initialValue += transform.localPosition.x - this.lastKnownValue;
                }
                transform.localPosition = new Vector3(this.initialValue + waveRes, transform.localPosition.y, transform.localPosition.z);
                this.lastKnownValue = transform.localPosition.x;

                break;
            case Movement.vertical:
                if (transform.localPosition.y != this.lastKnownValue)
                {
                    this.initialValue += transform.localPosition.y - this.lastKnownValue;
                }
                transform.localPosition = new Vector3(transform.localPosition.x, this.initialValue + waveRes, transform.localPosition.z);
                this.lastKnownValue = transform.localPosition.y;
                break;
            case Movement.depth:
                if (transform.localPosition.z != this.lastKnownValue)
                {
                    this.initialValue += transform.localPosition.z - this.lastKnownValue;
                }
                transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, this.initialValue + waveRes);
                this.lastKnownValue = transform.localPosition.z;
                break;
            case Movement.scale:
                var v = this.initialValue + waveRes;
                transform.localScale = new Vector3(v, v, v);
                break;
            case Movement.scaleX:
                transform.localScale = new Vector3(this.initialValue + waveRes, transform.localScale.y, transform.localScale.z);
                break;
            case Movement.scaleY:
                transform.localScale = new Vector3(transform.localScale.x, this.initialValue + waveRes, transform.localScale.z);
                break;
            case Movement.scaleZ:
                transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y, this.initialValue + waveRes);
                break;
            case Movement.eulerX:
                if (transform.localEulerAngles.x != this.lastKnownValue)
                {
                    this.initialValue = this.initialValue + (transform.localEulerAngles.x - this.lastKnownValue);
                }
                //    绕转x 轴 有 取值范围限制范围，因此这样写
                var val = this.initialValue + waveRes;
                val = val % 180;
                if (val > 90)
                {
                    val = val - 180;
                }
                else if (val < -90)
                {
                    val = val + 180;
                }

                var vector3 = new Vector3(val, transform.localEulerAngles.y, transform.localEulerAngles.z);
                transform.localEulerAngles = vector3;
                this.lastKnownValue = transform.localEulerAngles.x;
                break;
            case Movement.eulerY:
                //计算欧拉角
                if (transform.localEulerAngles.y != this.lastKnownValue)
                {
                    this.initialValue = this.initialValue + (transform.localEulerAngles.y - this.lastKnownValue);
                }

                transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, this.initialValue + waveRes, transform.localEulerAngles.z);
                this.lastKnownValue = transform.localEulerAngles.y;
                break;
            case Movement.eulerZ:
                if (transform.localEulerAngles.z != this.lastKnownValue)
                {
                    this.initialValue = this.initialValue + (transform.localEulerAngles.z - this.lastKnownValue);
                }

                transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y, this.initialValue + waveRes);
                this.lastKnownValue = transform.localEulerAngles.z;
                break;

        }

    }

}
